﻿
<%@ Page %>
<html>
<head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312"/>
<title>增加列</title>
<link rel="stylesheet" type="text/css" href="/_common/styles/global.css">
<link rel="stylesheet" type="text/css" href="/_common/styles/dialogs.css">
<style>

	TD.gridspacer
	{
		width:				2px;
	}

	TD.gridheader
	{
		padding-left:		8px;
	}

	TABLE.gridheader
	{
		background-color:	#e9e7d7;
		background-image:	url(/_grid/imgs/bar_bk.gif);
		font-size:			11px;
		width:				100%;
		height:				22px;
		border:				1px solid #dbdac9;
		border-bottom:		1px solid #c5c1b1;
	}

</style>

<script language="javascript" src="/_controls/settings.aspx"></script>
<script language="javascript" src="/_common/scripts/global.js"></script>
<script language="javascript" src="/Tools/ViewEditor/Scripts/objects.js"></script>	

<script language="JavaScript">

	var	_oLastGlow;

	function window.onload()
	{
		var oXml = new ActiveXObject("Msxml2.DOMDocument");
			oXml.async = false;

		var oXsl = new ActiveXObject("Microsoft.XMLDOM");
			oXsl.async = false;
			
		// build the list of available fields, grab the fields xsl from the form directory
		//
		oXsl.load("/Tools/FormEditor/Dialogs/fields.xsl");
		
		// Load the Fields Xml
		//
		oXml.loadXML(GetFieldsXml());

		// Display the results to the user
		//
		FieldList.innerHTML = oXml.transformNode(oXsl);
	}


	function Ok()
	{
		var oFields = new Array();
		var oDisplayNames;

		var i = 0;
		var ii = 0;
		var iLen = Fields.rows.length;

		while (i < iLen)
		{
			if (Fields.rows[i].cells[0].firstChild.checked)
			{
				oDisplayNames = new Array(new LocalizedObj(Fields.rows[i].cells[1].innerText, ORG_LANGUAGE_CODE));
				
				oFields[ii]	= new FieldObj(
								Fields.rows[i].cells[2].innerText,
								Fields.rows[i].cells[3].innerText,
								(Fields.rows[i].required == "true"),
								Fields.rows[i].orgrequired,
								oDisplayNames,
								oDisplayNames,
								null,
								null
							);
				ii++;
			}

			i++;
		}
		
		window.returnValue = (!oFields.length) ? false : oFields;
		window.close();
	}


	// returns a sting of xml containing all of the available fields
	//
	function GetFieldsXml()
	{
		with (window.dialogArguments)
		{
			var oGridXml		= GridXml;
			var oFieldsXml		= FieldsXml;
			var oPropertiesXml	= FieldPropertiesXml;
		}

		var sFieldName;
		var sFilter = BuildUsedFieldFilter(oGridXml.selectNodes("/grid/row/cell"));

		// get all the field nodes from the field properties xml minus the fields already in use by the grid
		var oFields = oPropertiesXml.selectNodes("/entity/fields/field" + ((sFilter.length > 0) ? "[" + sFilter + "]" : ""));

		// add each unused field to the fields xml which will later be passed to the add fields dialog
		var sFieldsXml = "<fields>";

		var i;
		var iLen = oFields.length;

		for (i = 0; i < iLen; i++)
		{
			sFieldName = oFields[i].getAttribute("name");
			if (sFieldName != "")
			{
				sFieldsXml += "<field name='" + sFieldName + "' displayname='" + Encode(oFields[i].selectSingleNode("displaynames/displayname[@languagecode = " + ORG_LANGUAGE_CODE + "]/@description").text) + "' datatype='" + oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@datatype").text + "' required='" + oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@validforgrid").text + "' orgrequired='" + oPropertiesXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@requiredlevel").text + "'/>";
			}
		}
		
		return sFieldsXml + "</fields>";
	}


	function Encode(s)
	{
		return s.replace(/\x27/g, "&#x27;");
	}


	function BuildUsedFieldFilter(oNodes)
	{
		var sFilter = "";

		var i;
		var iLen = oNodes.length;

		for (i = 0; i < iLen; i++)
		{
			if (i == 0)
			{
				sFilter += "@name != '" + oNodes[i].getAttribute("name") + "'";
			}
			else
			{
				sFilter += " and @name != '" + oNodes[i].getAttribute("name") + "'";
			}
		}

		return sFilter;
	}


	function SelectAll()
	{
		var o = event.srcElement;
		var bIsChecked = o.checked;
		var iLen = Fields.rows.length;
		
		for (i = 0; i < iLen; i++)
		{
			Fields.rows[i].cells[0].children[0].checked = bIsChecked;
		}
	}


	function on()
	{
		_oLastGlow = event.srcElement;

		while (_oLastGlow.tagName !=  "TR" )
		{
			_oLastGlow = _oLastGlow.parentElement;
		}

		_oLastGlow.runtimeStyle.backgroundColor = "#ADC3E7";
	}


	function off()
	{
		if (_oLastGlow)	_oLastGlow.runtimeStyle.backgroundColor = "";
	}
	
</script>
</head>

	<table style="width:100%; height:100%;" cellspacing="0" cellpadding="8">
		<tr>
			<td class="header">
				<div class="header">增加列</div>
				<div class="headerdesc">选择增加到视图的列。</div>
			</td>
		</tr>
		<tr>
			<td class="main">
				<table width="100%" cellspacing="0" cellpadding="0">
					<tr height="22">
						<td>
							<table cellpadding="0" cellspacing="0" class="gridheader">
								<tr>
									<td width="24" style="padding-left: 4px;"><input type="checkbox" class="checkbox" onclick="SelectAll();"></td>
									<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
									<td class="gridheader"><!-Field Name->显示名</td>
									<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
									<td class="gridheader" width="168">字段名</td>
									<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
									<td class="gridheader" width="64"><!-Type->类型</td>
									<td class="gridspacer"><img src="/_grid/imgs/bar_line.gif"></td>
									<td width="14">&nbsp;</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<div id="FieldList" style="background-color: #ffffff; height: 250px; border-right: 1px solid #cccccc;border-left: 1px solid #cccccc; border-bottom: 1px solid #cccccc; overflow-y: scroll; overflow-x: hidden;"></div>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td class="buttons">
				<button id="btnDialogOk" onclick="Ok();">确定</button>
				&nbsp;
				<button onclick="window.close()">取消</button>
			</td>
		</tr>
	</table>

</body>
</html>
